import { shuffle } from '@/utils'

export default {
  // 改变当前播放的歌曲的索引
  changecurrentindex (state, index) {
    state.currentindex = index
  },
  // 改变顺序列表
  changesquencelist (state, list) {
    state.squencelist = list
  },
  // 改变当前播放的列表
  changeplaylist (state, list) {
    state.playlist = list
  },
  // 改变当前播放状态
  changeplaying (state) {
    state.playing = !state.playing
  },
  // 状态改为播放
  toplaying (state) {
    state.playing = true
  },
  tonotplaying (state) {
    state.playing = false
  },
  // 改变播放顺序
  changemode (state) {
    const newmode = (state.playmode + 1) % 3
    const currentsong = state.playlist[state.currentindex]
    if (newmode === 1) {
      state.playlist = shuffle(state.squencelist)
      state.currentindex = state.playlist.findIndex(
        (item) => item.id === currentsong.id
      )
    }
    if (newmode === 0) {
      state.currentindex = state.squencelist.findIndex(
        (item) => item.id === currentsong.id
      )
      state.playlist = state.squencelist
    }
    state.playmode = newmode
  },
  // 下一首歌
  playnext (state) {
    state.currentindex = (state.currentindex + 1) % state.playlist.length
  },
  // 上一首歌
  playpre (state) {
    state.currentindex =
      (state.currentindex + state.playlist.length - 1) % state.playlist.length
  },
  // 添加或取消当前歌曲到收藏列表
  changecollect (state, song) {
    const index = state.collectlist.findIndex((item) => item.id === song.id)
    if (index === -1) {
      state.collectlist.push(song)
    } else {
      state.collectlist.splice(index, 1)
    }
  },
  addsonglyric (state, { song, lyric }) {
    if (song.lyric) return
    song.lyric = lyric
  },
  changelyric (state, lyric) {
    state.lyric = lyric
  },
  changelyricindex (state, index) {
    state.lyricindex = index
  },
  changefullscreen (state, bo) {
    state.fullscreen = bo
  },
  // 删除一个歌曲
  deleteone (state, song) {
    const index = state.playlist.findIndex((item) => item.id === song.id)
    console.log(index)
    if (index === -1) return
    state.playlist.splice(index, 1)
    state.squencelist.splice(index, 1)
    if (index < state.currentindex || index === state.squencelist.length) {
      state.currentindex--
    }
  },
  // 添加一个搜索历史
  addhistory (state, his) {
    const index = state.searchHistory.findIndex((item) => item === his)
    if (index !== -1) return
    state.searchHistory.push(his)
  },
  deleteonehis (state, his) {
    const index = state.searchHistory.findIndex((item) => item === his)
    if (index !== -1) return
    state.searchHistory.splice(index, 1)
  },
  clearhistory (state) {
    state.searchHistory = []
  },
  // 添加一首播放历史
  addhistorymusic (state, song) {
    const index = state.playhistorylist.findIndex((item) => item.id === song.id)
    if (index > -1) {
      state.playhistorylist.splice(index, 1)
    }
    state.playhistorylist.unshift(song)
  }
}
